package com.wpre.springblog;


import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/param")
public class ParamController {
    @RequestMapping("/p1")
    public String p1(String name) {
        return name;
    }

    @RequestMapping("/p2")
    public String p2(int age) {
        return "接收参数" + age;
    }

    //RequestMapping可以接收get、和set请求
    @RequestMapping(value = "/p3")
    public String p3() {
        return "p3";
    }

    //设置参数method = RequestMethod.GET,表示只接收get请求
    @RequestMapping(value = "/p4", method = RequestMethod.GET)
    public String p4() {
        return "p4";
    }

    //设置参数method = RequestMethod.POST,表示只接收post请求
    @RequestMapping(value = "/p5", method = RequestMethod.POST)
    public String p5() {
        return "p5";
    }

    //GetMapping,只接收get请求
    @GetMapping("/p6")
    public String p6() {
        return "p6";
    }

    //PostMapping,只接收post请求
    @PostMapping("/p7")
    public String p7() {
        return "p7";
    }

//请求:接收请求中的参数

    //1个参数
    @RequestMapping("/p8")
    public String p8(String name) {
        return "name: " + name;
    }

    //多个参数
    @RequestMapping("/p9")
    public String p9(String name, Integer age) {
        //尽量使用包装类,因为前端传递的普通参数默认是String类型,后端接收时,使用包装类可以进行类型转换
        return "name: " + name + ", age: " + age;
    }

    @RequestMapping("/p10")
    public String p10(Boolean b) {
        //前端传递0->false,1->true
        return "b: " + b;
    }

    //使用对象接收,多个参数
    @RequestMapping("/p11")
    public String p11(User user) {
        return "user: " + user;
    }

    //给前端的传递过来的参数重命名
    @RequestMapping("/p12")
    public String p12(@RequestParam("username") String name) {
        //前端的参数名字是username,我们后端定义的是name,@RequestParam的作用就是参数绑定
        //如果加上了这个注解,前端的参数名字必须和@RequestParam()括号内的名字一样,否则会报错
        return "name:" + name;
    }

    @RequestMapping("/p13")
    public String p13(@RequestParam(value = "username", required = false) String name) {
        //@RequestParam的required默认是true,表示必传参数,前端没有传这个参数会报错,
        //设置为false后,即使前端的参数为name,也不会报错
        return "name:" + name;
    }

    //使用数组接收
    //前端如果传递像  arr=张三&arr=李四  这样的重复的参数,会把它们封装成一个数组
    //前端的参数 arr=张三,李四,王五 此时数组长度是3(会自动切割)
    @RequestMapping("/p14")
    public String p14(String[] arr) {
        return "arr: " + List.of(arr);
    }

    //使用集合接收
    @RequestMapping("/p15")
    public String p15(@RequestParam List<String> list) {
        //前端传递的同样是数组,我们后端需要加上@RequestParam把前端传递的数组绑定成List
        //所以说,RequestParam既可以绑定参数名,也可以绑定类型
        return "list: " + list;
    }

    //传递json数据
    @RequestMapping("/p16")
    public String p16(@RequestBody User user) {
        //RequestBody,表示从请求正文中拿到数据
        return "user: " + user;
    }

    //获取url中的参数(上述的例子都是从查询字符串或者body中获取的)
    @RequestMapping("/article/{articleId}")
    public String p17(@PathVariable String articleId) {
        return "article" + articleId;
    }

    //2个参数
    @RequestMapping("/article/{articleId}/{name}")
    public String p18(@PathVariable String articleId, @PathVariable String name) {
        return "article" + articleId + ",   name" + name;
    }


    //获取上传的文件
    @RequestMapping("/p19")
    public String p19(@RequestPart("fileName") MultipartFile file) {
        String name = file.getOriginalFilename();
        return "file: " + name;
    }

    //获取Cookie:传统方式,利用HttpServletRequest
    @RequestMapping("/p20")
    public String p20(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        if (cookies == null) {
            //Cookie可能为空
            return "Cookie为空";
        }
        for (Cookie cookie : cookies) {
            System.out.println("name: " + cookie.getName() + " value: " + cookie.getValue());
        }
        return "获取到Cookie";
    }

    //注解的方式获取Cookie
    @RequestMapping("/p21")
    public String p21(@CookieValue("Vampire") String Vampire) {
        //@CookieValue(),括号中填写想要获取的key,获取到的key的value会给方法中的参数Vampire
        return "获取到Cookie,Vampire的值为 " + Vampire;
    }


    //设置Session
    //session是存储在内存中的,重启服务器会丢失session,所以要设置Session
    @RequestMapping("/setSession")
    public String setSession(HttpServletRequest request) {
        HttpSession session = request.getSession();//先获取到Session,如果没有会创建一个
        session.setAttribute("username", "张三");//设置session,key-value
        session.setAttribute("age", 18);//设置session,key-value
        return "设置Session成功";
    }


    //获取Session
    @RequestMapping("/p22")
    public String p22(HttpServletRequest request) {
        //原理: 从Cookie中找到SessionID,根据SessionID拿到Session

        HttpSession session = request.getSession();
        // HttpSession session1 = request.getSession(true);
        // getSession有参数版本:true->如果Session对象为空会创建一个Session对象;
        //                    false->如果Session对象为空,不会创建Session对象,
        //                    getSession默认为true

        //Session是类似Map的结构
        Object username = session.getAttribute("username");//可以理解为根据key获取value
        Object age = session.getAttribute("age");

        if (username == null || age == null) {
            return "null";
        }
        return "name: " + username + ", age: " + age;
    }

    //第二种获取Session的方法
    @RequestMapping("/p23")
    public String p23(HttpSession session) {

        Object username = session.getAttribute("username");
        Object age = session.getAttribute("age");

        if (username == null || age == null) {
            return "null";
        }
        return "name: " + username + ", age: " + age;
    }

    //第三种获取Session:使用注解
    @RequestMapping("/p24")
    public String p24(@SessionAttribute String username) {

        return "获取Session信息,username: " + username;
    }


    //获取Header:使用HttpServletRequest
    @RequestMapping("/p25")
    public String p25(HttpServletRequest request) {
        String UserAgent = request.getHeader("User-Agent");
        String ContentType = request.getHeader("Content-Type");
        return "UserAgent: " + UserAgent + ", ContentType: " + ContentType;
    }

    //获取Header:使用注解
    @RequestMapping("/p26")
    public String p26(@RequestHeader("User-Agent") String UserAgent) {
        return "UserAgent: " + UserAgent;
    }

}
